package com.ucode.cgms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.ucode.cgms.mapper.CgmsCompanionOfflineOrderReportMapper;
import com.ucode.cgms.service.CgmsCompanionOfflineOrderReportService;
import com.ucode.model.cgms.CgmsCompanionOfflineOrderReport;
import com.ucode.model.cgms.enums.CgmsCompanionReportAuditStateEnum;
import com.ucode.model.cgms.enums.CgmsSysResultCodeEnum;
import com.ucode.tool.base.enums.GlobalResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;
import com.ucode.tool.util.IdentifyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * 陪玩线下订单报备服务实现类
 * @Author: liliang
 * @Date: 2025/5/11 14:35
 * @Version: 1.0
 */
@Service
public class CgmsCompanionOfflineOrderReportServiceImpl implements CgmsCompanionOfflineOrderReportService {

    @Resource
    private CgmsCompanionOfflineOrderReportMapper cgmsCompanionOfflineOrderReportMapper;

    @Override
    public CgmsCompanionOfflineOrderReportMapper initMapper() {
        return cgmsCompanionOfflineOrderReportMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(CgmsCompanionOfflineOrderReport entity) {
        if(ObjectUtil.isNull(entity.getId())){
            entity.setId(IdentifyUtils.getDefaultSnowflakeId());
        }
        int rows = cgmsCompanionOfflineOrderReportMapper.insert(entity);
        return rows > 0 ? entity.getId() : null;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(CgmsCompanionOfflineOrderReport entity) {
        return cgmsCompanionOfflineOrderReportMapper.update(entity);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int logicDeletePrimaryKey(Long id) {
        return cgmsCompanionOfflineOrderReportMapper.logicDeletePrimaryKey(id);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int logicDeleteByPrimaryKeys(Collection<Long> ids) {
        return cgmsCompanionOfflineOrderReportMapper.logicDeleteByPrimaryKeys(ids);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int logicDelete(Long id) {
        return this.logicDeletePrimaryKey(id);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchLogicDelete(Collection<Long> ids) {
        return this.logicDeleteByPrimaryKeys(ids);
    }
    
    @Override
    public CgmsCompanionOfflineOrderReport selectByPrimaryKey(Long id) {
        return cgmsCompanionOfflineOrderReportMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean auditReport(Long id, Integer auditState, Long processorId, Integer processorType, String processRemark) {
        // 验证报备ID存在性
        CgmsCompanionOfflineOrderReport report = this.validate(id);
        
        // 验证审核状态合法性
        CgmsCompanionReportAuditStateEnum auditStateEnum = CgmsCompanionReportAuditStateEnum.getByCode(auditState);
        if (auditStateEnum == null) {
            throw new UcodeServiceException(GlobalResultCodeEnum.VALIDATE_FAILED, "审核状态不合法");
        }
        
        // 执行审核操作
        int result = cgmsCompanionOfflineOrderReportMapper.auditReport(
            id, auditState, processorId, processorType, LocalDateTime.now(), processRemark);
        
        return result > 0;
    }
    
    /**
     * 获得自身的代理对象，解决 AOP 生效问题
     *
     * @return 自己
     */
    private CgmsCompanionOfflineOrderReportServiceImpl getSelf() {
        return SpringUtil.getBean(getClass());
    }
} 